package com.jsmile.cloud.msgcenter.msg.model;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.jsmile.cloud.msgcenter.api.constants.MsgConstants;
import com.jsmile.cloud.msgcenter.api.req.ReqNotifyCreate;
import com.jsmile.cloud.msgcenter.msg.event.NotifyCreateEvent;
import com.jsmile.mail.security.event.DomainEventPublisher;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author 龚亮
 * @version 1.0
 * @desc 为了薪资破万，还是睡一会儿吧...!
 * @date 2020年06月07日 11:17:04
 */
@ApiModel
@Data
@TableName("t_ms_notify")
public class MsNotify extends Model<MsNotify> {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "id")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
    @ApiModelProperty(value = "通知标题")
    private String notifyTitle;
    @ApiModelProperty(value = "消息的内容")
    private String notifyContent;
    @ApiModelProperty(value = "消息的跳转url")
    private String notifyUrl;
    @ApiModelProperty(value = "消息的类型 01业务消息 02系统公告 03升级公告")
    private String msgType;
    @ApiModelProperty(value = "目标的ID(比如文章ID)")
    private Long targetId;
    @ApiModelProperty(value = "目标的类型(如工单order,文章post)")
    private String targetType;
    @ApiModelProperty(value = "动作类型(评论:comment,喜欢:like,通知:notify)")
    private String action;
    @ApiModelProperty(value = "发送者ID")
    private Integer senderId;
    @ApiModelProperty(value = "发送者类型(普通用户user,管理员admin)")
    private String senderType;
    @ApiModelProperty(value = "remark")
    private String remark;
    @ApiModelProperty(value = "创建时间")
    private java.util.Date createTime;
    @ApiModelProperty(value = "创建者")
    private Long creator;
    @ApiModelProperty(value = "创建者")
    private String createBy;
    @ApiModelProperty(value = "更新时间")
    private java.util.Date updateTime;
    @ApiModelProperty(value = "更新者")
    private String updateBy;
    @ApiModelProperty(value = "更新者")
    private Long updater;
    @ApiModelProperty(value = "租户id")
    private Long tenantId;
    @ApiModelProperty(value = "平台id")
    private Integer platformId;
    @ApiModelProperty(value = "业务id")
    private Integer bizCode;

    @TableField(exist = false)
    @ApiModelProperty(value = "消息接收者")
    private List<MsUserNotify> msUserNotifies;

    public static MsNotify create(ReqNotifyCreate reqNotifyCreate) {
        MsNotify msNotify = BeanUtil.toBean(reqNotifyCreate, MsNotify.class);
        msNotify.setId(IdUtil.getSnowflake(MsgConstants.SNOWFLAKE.workerId, MsgConstants.SNOWFLAKE.datacenterId).nextId());
        msNotify.setCreator(reqNotifyCreate.getCurUserId());
        msNotify.setCreateTime(new Date());
        msNotify.setTenantId(reqNotifyCreate.getCurTenantId());
        msNotify.setMsUserNotifies(MsUserNotify.create(msNotify, reqNotifyCreate));
        DomainEventPublisher.publish(new NotifyCreateEvent(msNotify));
        return msNotify;
    }
}
